package com.yanze.schoolinformation.view.login_register.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentRegisterBinding;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.SPUtils;
import com.yanze.schoolinformation.utils.StringUtils;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.login.RegisterNavigator;
import com.yanze.schoolinformation.viewModule.login.RegisterViewModel;

/**
 * Created by MrZheng on 2018/5/7.
 */

public class RegisterFragment extends BaseFragment<FragmentRegisterBinding> implements RegisterNavigator {

    private RegisterViewModel viewModel;

    @Override

    public int setContent() {
        return R.layout.fragment_register;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();
        initView();
    }

    private void initView() {
        viewModel = new RegisterViewModel(this);
        viewModel.setmNavigator(this);
        MyOnClick onclick = new MyOnClick();
        bindingView.ivBack.setOnClickListener(onclick);
        bindingView.btnRegister.setOnClickListener(onclick);
    }

    @Override
    public void registerSuccess(RespUser user) {
        DialogUtils.dismissDialog();
        ToastUtils.show(getContext(), "注册成功");
        SPUtils.put(getContext(), C.key.SP_Phone,user.getData().getUserphone());
        toLogin();
    }

    @Override
    public void registerFaild(String msg) {
        ToastUtils.show(getContext(), msg);
        DialogUtils.dismissDialog();
    }

    private class MyOnClick extends PerfectClickListener {

        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.iv_back:
                    toLogin();
                    break;
                case R.id.btn_register:
                    String phone = bindingView.cetPhone.getText().toString();
                    String pwd1 = bindingView.petPwd1.getText().toString();
                    String pwd2 = bindingView.petPwd2.getText().toString();
                    if (!checkRegister(phone, pwd1, pwd2)) {
                        return;
                    }
                    DialogUtils.showDialog(getContext(),"请稍候...");
                    viewModel.Register(phone,pwd1);
                    break;
            }
        }
    }

    //验证码,密码格式校验
    private boolean checkRegister(String phone, String pwd1, String pwd2) {
        if (StringUtils.isEmpty(phone)) {
            bindingView.inputPhone.setError(getString(R.string.please_input_phoneNumber));
            return false;
        }
        if (!StringUtils.isPhoneNumber(phone)) {
            bindingView.inputPhone.setError(getString(R.string.phone_format_error));
            return false;
        }
        bindingView.inputPhone.setError("");
        bindingView.inputPhone.setErrorEnabled(false);
        if (StringUtils.isEmpty(pwd1) || StringUtils.isEmpty(pwd2)) {
            bindingView.inputPwd1.setError(getString(R.string.please_input_pwd));
            return false;
        }
        if (!pwd1.equals(pwd2)) {
            bindingView.inputPwd2.setError(getString(R.string.two_input_pwd_nosame));
            return false;
        }
        bindingView.inputPwd1.setError("");
        bindingView.inputPwd2.setError("");
        bindingView.inputPwd1.setErrorEnabled(false);
        bindingView.inputPwd2.setErrorEnabled(false);
        return true;
    }

    //去往登录界面
    private void toLogin() {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}
